package com.bluefish.bltuan.action.admin;

import java.io.File;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.AbstractAction;
import com.bluefish.bltuan.annotation.Before;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.Image;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.ImageService;
import com.bluefish.bltuan.service.ShopService;
import com.bluefish.bltuan.tools.DateUtils;
import com.bluefish.bltuan.tools.shop.AccordType;
import com.bluefish.bltuan.util.PageIndex;

@Results({
	@Result(name = "list", type = "redirect", location = "/admin/shop"),
	@Result(name = "today", type = "redirect", location = "/admin/shop"),
	@Result(name = "past", type = "dispatcher", location = "past.jsp"),
	@Result(name = "future", type = "dispatcher", location = "future.jsp"),
	@Result(name = "futureList", type = "redirect", location = "/admin/shop/of/future", params = {"cityId","${cityId}"})
})
public class ShopAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8141755173976444494L;

	private ShopService shopService;
	
	private ImageService imageService;
	
	private CategoryService categoryService;
	
	private List<TuanGou> tuans;

	private String address;

	private float currentPrice;

	private String detail;

	private float originalPrice;

	private float discount;

	private String light;

	private int limitPerson;

	private String name;

	private String nick;

	private String simpleDescribe;

	private String tip;
	
	private TuanGou currentTuan;

	private File image;
	
	private String imageFileName;
	
	private String from;

	private String traffic;

	private String seller;

	private String longitude;

	private String latitude;

	private String mapLink;

	private Integer cityId;

	private int mapLevel;
	
	private List<Category> cities;
	
	@Resource
	private String mapPicLinkFormat;

	private Date expireDate;

	private Date publishTime;

	private boolean published;
	
	private Category currentCity;

	private String website;
	
	private AccordType accordType;
	
	private static final int WIDTH = 408;
	
	private static final int HEIGHT = 260;
	
	@Before(include={"edit", "update", "destroy","publish","unpublish"})
	public void prepare(){
		if(this.getId() != null)
			this.currentTuan = this.shopService.findById(getIntegerId());
	}
	
	@Before(include = {"edit", "editNew", "past", "future", "today"})
	public void loadCity(){
		if(this.cityId != null)
			this.currentCity = this.categoryService.findById(this.cityId);
	}
	
	public String publish(){
		boolean success = this.shopService.publish(this.currentTuan);
		addFlashMessage(getText(success?"publish":"cannotPublish"));
		redirect();
		return null;
	}
	
	public String unpublish(){
		this.shopService.unpublish(this.currentTuan);
		addFlashMessage(getText("unpublish"));
		redirect();
		return null;
	}
	
	@Override
	public HttpHeaders create() {
		this.currentTuan = new TuanGou();
		setProperties(this.currentTuan);
		this.shopService.save(this.currentTuan);
		return new DefaultHttpHeaders("futureList").disableCaching();
	}

	@Override
	public String destroy() {
		this.shopService.delete(this.currentTuan);
		redirect();
		return null;
	}

	@Override
	public String edit() {
		return EDIT;
	}

	@Override
	public HttpHeaders editNew() {
		return new DefaultHttpHeaders(NEW).disableCaching();
	}
	
	@Override
	public HttpHeaders index() {
		PageIndex pageIndex = getPageIndex();
		this.cities = this.categoryService.listCities(pageIndex);
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	public HttpHeaders past() {
		PageIndex pageIndex = getPageIndex();
		this.tuans = this.shopService.listOldTuangou(pageIndex, this.currentCity);
		return new DefaultHttpHeaders("past").disableCaching();
	}
	
	public HttpHeaders future() {
		PageIndex pageIndex = getPageIndex();
		this.tuans = this.shopService.listFutureTuangou(pageIndex, this.currentCity);
		return new DefaultHttpHeaders("future").disableCaching();
	}

	@Override
	public HttpHeaders show() {
		return null;
	}
	
	public HttpHeaders today() {
		this.currentTuan = this.shopService.getTuangouToday(this.currentCity);
		if(this.currentTuan == null){
			this.currentTuan = new TuanGou();
			this.currentTuan.setPublishTime(new Date());
			this.currentTuan.setPublishedDate(
					DateUtils.getDateString(this.currentTuan.getPublishTime()));
		}
		return new DefaultHttpHeaders(EDIT).disableCaching();
	}

	@Override
	public String update() {
		setProperties(this.currentTuan);
		this.shopService.saveOrUpdate(this.currentTuan);
		return this.from;
	}
	
	private void setProperties(TuanGou tuanGou){
		tuanGou.setName(name);
		tuanGou.setNick(nick);
		tuanGou.setSeller(seller);
		tuanGou.setWebsite(website);
		tuanGou.setSimpleDescribe(simpleDescribe);
		tuanGou.setCurrentPrice(currentPrice);
		tuanGou.setOriginalPrice(originalPrice);
		tuanGou.setDiscount(discount);
		tuanGou.setPublishTime(publishTime);
		tuanGou.setPublishedDate(DateUtils.getDateString(publishTime));
		tuanGou.setPublished(published);
		tuanGou.setLimitPerson(limitPerson);
		tuanGou.setLight(light);
		tuanGou.setTip(tip);
		tuanGou.setAccordType(accordType);
		tuanGou.setDetail(detail);
		tuanGou.setAddress(address);
		tuanGou.setTraffic(traffic);
		tuanGou.setExpireDate(expireDate);
		tuanGou.setExpireTime(DateUtils.getDateString(expireDate));
		tuanGou.setLongitude(longitude);
		tuanGou.setLatitude(latitude);
		tuanGou.setMapLevel(mapLevel);
		tuanGou.setMapPicLink(getMapPicLink());
		tuanGou.setMapLink(mapLink);
		tuanGou.setSaved(originalPrice-currentPrice);
		if(this.cityId != null){
			Category currentCity = this.categoryService.findById(this.cityId);
			tuanGou.setCity(currentCity);
		}
		if(this.image != null){
			Image headImg = this.imageService.saveImage(this.image
					, imageFileName, WIDTH, HEIGHT);
			tuanGou.setHeadImg(headImg);
		}
	}
	
	public AccordType[] getAccordTypes() {
		return AccordType.values();
	}
	
	private String getMapPicLink() {
		return MessageFormat.format(this.mapPicLinkFormat, this.longitude,
				this.latitude, this.mapLevel);
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}
	
	public List<TuanGou> getTuans() {
		return tuans;
	}
	
	public TuanGou getCurrentTuan() {
		return currentTuan;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setCurrentPrice(float currentPrice) {
		this.currentPrice = currentPrice;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public void setOriginalPrice(float originalPrice) {
		this.originalPrice = originalPrice;
	}

	public void setDiscount(float discount) {
		this.discount = discount;
	}

	public void setLight(String light) {
		this.light = light;
	}

	public void setLimitPerson(int limitPerson) {
		this.limitPerson = limitPerson;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public void setSimpleDescribe(String simpleDescribe) {
		this.simpleDescribe = simpleDescribe;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}
	
	public void setImage(File image) {
		this.image = image;
	}
	
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	
	public void setFrom(String from) {
		this.from = from;
	}
	
	public String getFrom() {
		return from;
	}

	public void setTraffic(String traffic) {
		this.traffic = traffic;
	}

	public void setSeller(String seller) {
		this.seller = seller;
	}
	
	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}
	
	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}
	
	public void setMapLevel(int mapLevel) {
		this.mapLevel = mapLevel;
	}
	
	public void setMapLink(String mapLink) {
		this.mapLink = mapLink;
	}
	
	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}
	
	public Integer getCityId() {
		return cityId;
	}
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	public List<Category> getCities() {
		return cities;
	}
	
	public void setMapPicLinkFormat(String mapPicLinkFormat) {
		this.mapPicLinkFormat = mapPicLinkFormat;
	}
	
	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}
	
	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}
	
	public void setPublished(boolean published) {
		this.published = published;
	}
	
	public Category getCurrentCity() {
		return currentCity;
	}
	
	public void setWebsite(String website) {
		this.website = website;
	}

	public void setAccordType(int accordType) {
		this.accordType = AccordType.getType(accordType);
	}
	
}
